package com.linjiahao.userCenter.util;

import com.alibaba.fastjson.JSON;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @ClassName RandomUtil
 * @Description: 获取随机数
 * @Author linjiahao
 * @Date 29/12/2021
 **/
public class RandomUtil {

    private static final Random random = new Random();

    private static final DecimalFormat fourdf = new DecimalFormat("0000");

    private static final DecimalFormat sixdf = new DecimalFormat("000000");

    public static String getFourBitRandom() {
        return fourdf.format(random.nextInt(10000));
    }

    public static String getSixBitRandom() {
        return sixdf.format(random.nextInt(1000000));
    }

    /**
     * 给定数组，抽取n个数据
     *
     * @param list
     * @param n
     * @return
     */
    public static ArrayList getRandom(List list, int n) {

        Random random = new Random();

        HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

        // 生成随机数字并存入HashMap
        for (int i = 0; i < list.size(); i++) {

            int number = random.nextInt(100) + 1;

            hashMap.put(number, i);
        }

        // 从HashMap导入数组
        Object[] robjs = hashMap.values().toArray();

        ArrayList r = new ArrayList();

        // 遍历数组并打印数据
        for (int i = 0; i < n; i++) {
            r.add(list.get((int) robjs[i]));
            System.out.print(list.get((int) robjs[i]) + "\t");
        }
        System.out.print("\n");
        return r;
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data1 = "2021-12-01 13:56:56";
        String data5 = "2021-12-07 13:56:56";
        String data6 = "2021-12-09 13:56:56";
        String data2 = "2021-11-01 13:56:56";
        String data3 = "2021-1-01 13:56:56";
        String data4 = "2021-10-01 13:56:56";
        List<Date> list =new ArrayList<>();
        list.add(simpleDateFormat.parse(data1));
        list.add(simpleDateFormat.parse(data2));
        list.add(simpleDateFormat.parse(data3));
        list.add(simpleDateFormat.parse(data4));
        list.add(simpleDateFormat.parse(data5));
        list.add(simpleDateFormat.parse(data6));
        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM");
        String queryDate = "2021-12";
        List<Date> collect = list.stream().sorted().filter(m -> queryDate.equals(simpleDateFormat1.format(m))).collect(Collectors.toList());
        collect.stream().forEach( k ->{
            System.out.println(simpleDateFormat.format(k));
        });
    }
}
